package Dao;

import java.io.*;
import pod1_hoteis.Repositorio;


/**
 *
 * @author kassio
 * teste de commit
 */
public abstract class Dao<T> implements DaoIF<T> {
    

    
    private Repositorio repositorio;

    public Dao(){
        getRepositorio();
    }
    
    
    private String getFileName() {
        return "dados.dat";
    }
     
    public synchronized void salvarRepositorio() {

        try {
            String fileName = getFileName();
            FileOutputStream fout = new FileOutputStream(fileName);
            ObjectOutputStream objOut = new ObjectOutputStream(fout);
            objOut.writeObject(repositorio);
        }
        catch (Exception e) {
            System.out.println("ERRO: "+ e);
        }
    }
    
    public synchronized Repositorio getRepositorio() {
        try {
            String fileName = getFileName();
            FileInputStream fin = new FileInputStream(fileName);
            ObjectInputStream objIn = new ObjectInputStream(fin);
            repositorio = (Repositorio) objIn.readObject();
        } 
        catch (FileNotFoundException ex) {
            repositorio = Repositorio.getInstance();
            salvarRepositorio();
        }
        catch (NullPointerException ex) {
            repositorio = Repositorio.getInstance();
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }


        return repositorio;
    }



    
}
